package cn.ajb.filemanager.service;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.ajb.filemanager.entity.FileManager;
import cn.ajb.filemanager.util.Contant;
import cn.ajb.filemanager.util.FileUtil;
import cn.ajb.filemanager.util.qrcode.QRCodeUtil;

@Service
@Transactional
public class QRService {
	
	@Autowired
	private Contant contant;
	//创建二维码图片
	public void creatQrImage(FileManager filemanager,HttpServletRequest request){
		try {
			//创建内容
			String text=contant.getUrl()+"?id="+filemanager.getId();
			System.out.print(text);
			String parentFolderPath = "";
			String parentStrCn=filemanager.getParentStrCn();
			if(parentStrCn!=null && !"".equals(parentStrCn)){
				 parentFolderPath = parentStrCn.replace(" -> ", "/");
			}
			String fileName=filemanager.getFileName();
			String folderPath="";
			//保存文件到服务器
			if(filemanager.getFileType()==1){
				//说明是文件文件则放在其相关父级目录下
				 folderPath = request.getSession().getServletContext().getRealPath("/"+parentFolderPath);
				 //去除后缀名
				 int indexOf = fileName.indexOf(".");
				 fileName=fileName.substring(0,indexOf);
					
			}else{
				 folderPath = request.getSession().getServletContext().getRealPath("/"+parentFolderPath+"/"+fileName+"");
			}
			
			QRCodeUtil.encode(text, "", folderPath, true,"code");
			System.out.print("生成QR码成功");
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	//删除二维码图片
	public void deleteQRimage(FileManager filemanager,
			HttpServletRequest request) {
		/*//是文件则删除其二维码
		if(filemanager.getFileType()==1){
			String filePath=request.getSession().getServletContext().getRealPath("/"+filemanager.getParentStrCn());
			filePath=filePath+filemanager.getFileName()+".jpg";
			FileUtil.deleteFile(filePath);
		}*/
		
	}
	//修改文件的二维码
	public void updateQRImage(String oldFilePath, String newFilePath) {
		/*int oldIndex=oldFilePath.lastIndexOf(".");
		oldFilePath=oldFilePath.substring(0,oldIndex)+".jpg";
		int newIndex=newFilePath.lastIndexOf(".");
		newFilePath=newFilePath.substring(0,newIndex)+".jpg";
		FileUtil.updateFileName(oldFilePath, newFilePath);*/
		
	}
}
